/**
 * gome
 * created by 虚拟前端组 on 2016-06-03
 */
var root = window || {}, util = root.util;
root.Versions = "1";
var wrapEl, phoneEl, flowEl, Dd = function(a) {
    this.options = $.extend({phoneReg: /^\d{11}$/, canSendPhone: !1, canSendFlow: !1, loginUrl: "http://login.gome.com.cn/login.no", confirmUrl: "http://wt.gome.com.cn/recharge/confirminformation.html", message: {msg1: "请输入正确手机号", msg2: "未知地区和运营商", msg3: "请输入充值号码", msg4: "暂无商品"}, loadSelectPhone: "100元", loadSelectFlow: "1G", defselPhone: "", defselFlow: "", element: ""}, a || {}), wrapEl = this.options.element, phoneEl = $(".nav-phone", wrapEl), flowEl = $(".nav-flow", wrapEl), this.init()
};
$.extend(Dd.prototype, {init: function() {
        this.coreEvents(), this.getDefDataPhone(), this.getDefDataFlow()
    }, getLIELByText: function(a, b) {
        var c, d = a.find("li"), e = "";
        b = parseInt(b.replace(/g|G/g, "000"));
        for (var f = d.length; f > 0 && (c = d.eq(f - 1), e = parseInt(c.text().replace(/g|G/g, "000")), !(b >= e)); f--)
            ;
        return c
    }, getDefDataPhone: function() {
        var a = this;
        util.api({url: "presale", data: {rt: 501}, type: "get", success: function(b) {
                var c, d = b.rpco || 0, e = b.body || {}, f = e.grps || [], g = b.ulrph || [], h = "";
                if (200 == d) {
                    $.each(f, function(a, b) {
                        h += '<li class="sel-it" data-cid="' + b.cid + '" data-pri="¥' + b.pri + '">' + b.cnname + "</li>"
                    }), $('ul[name="phone-sel"]', phoneEl).html(h), a.options.defselPhone ? c = $('ul[name="phone-sel"]', phoneEl).find('.sel-it[data-cid="' + a.options.defselPhone + '"]') : (c = a.getLIELByText($('ul[name="phone-sel"]', phoneEl), a.options.loadSelectPhone), !c && (c = $('ul[name="phone-sel"]', phoneEl).find(".sel-it:eq(0)"))), $('input[name="dtccn"]', phoneEl).val(c.text()).attr({"data-cid": c.attr("data-cid")}), $('input[name="phonenum"]', phoneEl).attr({"data-ulrph": g.join(",")});
                    var i = c.attr("data-pri") || "";
                    $(".money", phoneEl).html(i)
                }
            }})
    }, getDefDataFlow: function() {
        var a = this;
        util.api({url: "presale", data: {rt: 601}, type: "get", success: function(b) {
                var c, d = b.rpco || 0, e = b.body || {}, f = e.grps || [], g = b.ulrph || [], h = "";
                if (200 == d) {
                    $.each(f, function(a, b) {
                        h += '<li class="sel-it" data-cid="' + b.cid + '" data-pri="¥' + b.pri + '">' + b.cnname + "</li>"
                    }), $('ul[name="flow-sel"]', flowEl).html(h), a.options.defselFlow ? c = $('ul[name="flow-sel"]', flowEl).find('.sel-it[data-cid="' + a.options.defselFlow + '"]') : (c = a.getLIELByText($('ul[name="flow-sel"]', flowEl), a.options.loadSelectFlow), !c && (c = $('ul[name="flow-sel"]', flowEl).find(".sel-it:eq(0)"))), $('input[name="dtccn"]', flowEl).val(c.text()).attr({"data-cid": c.attr("data-cid")}), $('input[name="phonenum"]', flowEl).attr({"data-ulrph": g.join(",")});
                    var i = c.attr("data-pri") || "";
                    $(".money", flowEl).html(i)
                }
            }})
    }, getDtsaleablePhone: function(a) {
        var b = this, c = $(".msg-info", phoneEl);
        util.api({url: "saleable", type: "get", data: {rt: 501, pn: a}, success: function(a) {
                b.options.canSendPhone = !1;
                var d = a.rpco || 0;
                if (40401 == d ? c.html(b.options.message.msg2).addClass("error-info") : 40402 == d && c.html(b.options.message.msg4).addClass("error-info"), 200 == d) {
                    var e = a.body || {}, f = (e.reg || "", e.op || "", e.ropn || ""), g = e.bid || "", h = e.rest || "", i = e.grps || [], j = "";
                    c.html(h + f).removeClass("error-info"), $.each(i, function(a, b) {
                        j += '<li class="sel-it" data-cid="' + b.cid + '">' + b.cnname + "</li>"
                    }), $('ul[name="phone-sel"]', phoneEl).html(j);
                    var k = b.getLIELByText($('ul[name="phone-sel"]', phoneEl), b.options.loadSelectPhone);
                    !k && (k = $('ul[name="phone-sel"]', phoneEl).find(".sel-it:eq(0)")), $('input[name="dtccn"]', phoneEl).val(k.text()).attr({"data-cid": k.attr("data-cid"), "data-bid": g}), b.getDtpricePhone()
                }
            }})
    }, getDtsaleableFlow: function(a) {
        var b = this, c = $(".msg-info", flowEl);
        util.api({url: "saleable", type: "get", data: {rt: 601, pn: a}, success: function(a) {
                b.options.canSendFlow = !1;
                var d = a.rpco || 0;
                if (40401 == d ? c.html(b.options.message.msg2).addClass("error-info") : 40402 == d && c.html(b.options.message.msg4).addClass("error-info"), 200 == d) {
                    var e = a.body || {}, f = (e.reg || "", e.op || "", e.ropn || ""), g = e.bid || "", h = e.rest || "", i = e.grps || [], j = "";
                    c.html(h + f).removeClass("error-info"), $.each(i, function(a, b) {
                        j += '<li class="sel-it" data-cid="' + b.cid + '">' + b.cnname + "</li>"
                    }), $('ul[name="flow-sel"]', flowEl).html(j);
                    var k = b.getLIELByText($('ul[name="flow-sel"]', flowEl), b.options.loadSelectFlow);
                    !k && (k = $('ul[name="flow-sel"]', flowEl).find(".sel-it:eq(0)")), $('input[name="dtccn"]', flowEl).val(k.text()).attr({"data-cid": k.attr("data-cid"), "data-bid": g}), b.getDtpriceFlow()
                }
            }})
    }, getDtpricePhone: function() {
        var a = this, b = $('input[name="phonenum"]', phoneEl).val() || "", c = $('input[name="dtccn"]', phoneEl).attr("data-bid") || "", d = $('input[name="dtccn"]', phoneEl).attr("data-cid") || "", e = $(".money", phoneEl);
        util.api({url: "predeal", type: "get", async: !1, data: {rt: 501, pn: b, bid: c, cid: d}, beforeSend: function() {
                e.html("正在查询价格...")
            }, success: function(b) {
                var c, d = b.rpco, f = b.body || {}, g = $('input[name="dtccn"]', phoneEl);
                switch (a.options.canSendPhone = !1, d) {
                    case 404:
                        e.html("暂无");
                        break;
                    case 200:
                        g.attr({"data-rt": f.rt, "data-cid": f.cid, "data-shopid": f.shopid}), c = f.sp, e.html("¥" + (Number(c) / 100).toFixed(2)), a.options.canSendPhone = !0;
                        break;
                    default:
                        e.html("查询失败")
                    }
            }})
    }, getDtpriceFlow: function() {
        var a = this, b = $('input[name="phonenum"]', flowEl).val() || "", c = $('input[name="dtccn"]', flowEl).attr("data-bid") || "", d = $('input[name="dtccn"]', flowEl).attr("data-cid") || "", e = $(".money", flowEl);
        util.api({url: "predeal", type: "get", async: !1, data: {rt: 601, pn: b, bid: c, cid: d}, beforeSend: function() {
                e.html("正在查询价格...")
            }, success: function(b) {
                var c, d = b.rpco, f = b.body || {}, g = $('input[name="dtccn"]', flowEl);
                switch (a.options.canSendFlow = !1, d) {
                    case 404:
                        e.html("暂无");
                        break;
                    case 200:
                        g.attr({"data-rt": f.rt, "data-cid": f.cid, "data-shopid": f.shopid}), c = f.sp, e.html("¥" + (Number(c) / 100).toFixed(2)), a.options.canSendFlow = !0;
                        break;
                    default:
                        e.html("查询失败！")
                    }
            }})
    }, isLogin: function() {
        var a, b, c = this;
        if (util.api({surl: root.API_PATH2 + "checklogin", type: "get", async: !1, success: function(c) {
                b = c.rpco, a = c.body || {}
            }}), 200 == b)
            return!0;
        if (401 == b) {
            var d = $(".nav-phone", wrapEl).is(":visible") > 0 ? phoneEl : flowEl, e = $('input[name="dtccn"]', d), f = $('input[name="phonenum"]', d).val(), g = e.attr("data-bid"), h = e.attr("data-cid"), i = (e.val(), e.attr("data-shopid")), j = 100 * Number($(".money", d).text().replace("¥", "")), k = $(this).parents(".nav-phone").length > 0 ? 501 : 601, l = $('input[name="dtccn"]', d).val(), m = util.encode("rt=" + k + "/pn=" + f + "/bid=" + g + "/cid=" + h + "/sid=" + i + "/sp=" + j + "/fv=" + l), n = c.options.confirmUrl + "?" + m, o = c.options.loginUrl + "?orginURI=" + n;
            window.open(o, "_blank")
        }
    }, updateUlPosition: function(a) {
        if (a) {
            var b = $(document).height(), c = a.closest(".sel-gro").find("ul"), d = 0, e = 0, f = a.closest(".sel-gro").find(".val-sel"), g = (f.offset().top, 0), h = !1;
            c.css({height: "auto", "margin-top": "0", "overflow-y": "hidden"}), d = c.offset().top, e = d + c.height(), e > b && (g = -1 * (e - b) - (b - c.height()) / 2), c.height() > b && (g = -1 * d + 5, h = !0), h ? c.css({height: b - 28 + "px", "overflow-y": "scroll"}) : 3 == c.find("li").length ? g -= 50 : c.find("li").length > 2 && (g -= 8), c.css({"margin-top": g + "px"})
        }
    }, coreEvents: function() {
        var a = this;
        $(document).on("click", function(a) {
            $(".sel-ls", wrapEl).hide()
        }), $(document).on("mouseleave", function(a) {
            $(".sel-ls", wrapEl).hide()
        }), wrapEl.on("click", ".sel-do:not(.disabled),.val-sel", function(b) {
            var c = $(this).closest("div").find("ul"), d = c.is(":hidden");
            $(".sel-ls", wrapEl).hide(), d && c.show(), a.updateUlPosition($(this)), b.stopPropagation()
        }), wrapEl.on("click", ".sel-ls li", function() {
            var b = $(this).text(), c = $(this).parents(".sel-gro").find(".val-sel").val(b), d = $(this).parents(".nav-phone").length > 0 ? phoneEl : flowEl, e = $("input[name=phonenum]", d).val();
            d.is(phoneEl) ? (c.attr("data-cid", $(this).attr("data-cid")), a.options.phoneReg.test(e) ? a.getDtpricePhone() : (a.options.defselPhone = $(this).attr("data-cid"), a.getDefDataPhone())) : (c.attr("data-cid", $(this).attr("data-cid")), a.options.phoneReg.test(e) ? a.getDtpriceFlow() : (a.options.defselFlow = $(this).attr("data-cid"), a.getDefDataFlow()))
        }), wrapEl.on("click", ".nav .item", function() {
            $(".nav .item", wrapEl).removeClass("item-sel"), $(this).addClass("item-sel");
            var a = $(this).attr("data-content");
            $(".nav-phone, .nav-flow", wrapEl).hide(), $("." + a, wrapEl).show()
        }), wrapEl.on("focus", "input[name=phonenum]", function() {
            $(this).val() == a.options.message.msg3 && $(this).val("")
        }), wrapEl.on("blur", "input[name=phonenum]", function() {
            if ($(this).val() == a.options.message.msg3 || "" == $(this).val()) {
                $(this).val(a.options.message.msg3);
                var b = $(this).parents(".inp-dtl").find(".msg-info");
                b.html("");
                var c = $(this).parents(".nav-phone").length > 0 ? phoneEl : flowEl;
                c.is(phoneEl) ? a.getDefDataPhone() : a.getDefDataFlow()
            }
        }), wrapEl.on("keyup", "input[name=phonenum]", function() {
            var b = $(this).val(), c = $(this).parents(".inp-dtl").find(".msg-info");
            if (0 == b.length) {
                var d = $(this).parents(".nav-phone").length > 0 ? phoneEl : flowEl;
                d.is(phoneEl) ? a.options.defselPhone = "" : a.options.defselFlow = "", $(this).blur()
            } else if (a.options.phoneReg.test(b)) {
                c.html("").removeClass("error-info");
                var d = $(this).parents(".nav-phone").length > 0 ? phoneEl : flowEl;
                d.is(phoneEl) ? ($('ul[name="phone-sel"]', phoneEl).html(""), a.getDtsaleablePhone(b)) : ($('ul[name="flow-sel"]', flowEl).html(""), a.getDtsaleableFlow(b))
            } else
                c.html(a.options.message.msg1).addClass("error-info")
        }), wrapEl.on("click", 'a[name="submit"]', function() {
            var b = $(this).parents(".nav-phone").length > 0 ? phoneEl : flowEl, c = $('input[name="phonenum"]', b).val();
            $(".money", b).text();
            if (a.options.phoneReg.test(c)) {
                if (b.hasClass("nav-phone")) {
                    if (!a.options.canSendPhone)
                        return
                } else if (b.hasClass("nav-flow") && !a.options.canSendFlow)
                    return;
                if (a.isLogin()) {
                    var d = $('input[name="dtccn"]', b), e = c, f = d.attr("data-bid"), g = d.attr("data-cid"), h = (d.val(), d.attr("data-shopid")), i = 100 * Number($(".money", b).text().replace("¥", "")), j = $(this).parents(".nav-phone").length > 0 ? 501 : 601, k = $('input[name="dtccn"]', b).val(), l = "rt=" + j + "/pn=" + e + "/bid=" + f + "/cid=" + g + "/sid=" + h + "/sp=" + i + "/fv=" + k;
                    l = util.encode(l);
                    var m = a.options.confirmUrl + "?" + l;
                    window.open(m, "_blank")
                }
            }
        })
    }}), $(document).ready(function() {
    new Dd({element: $(".tupandf")})
});